﻿@using KPIS.GERP.Models.ICM
@model IEnumerable<KPIS.GERP.Models.ICM.Received>

@{
    ViewBag.Title = "ขึ้นทะเบียนทรัพย์สิน [RC]";
    ViewBag.Descr = "รับพัสดุเข้าคลัง";
}


@using (Html.BeginForm("DeleteAll", "ReceiveParcel", FormMethod.Post, new { @id = "form" }))
{
    @Html.AntiForgeryToken()
    <div class="row-fluid">
        <div class="block-button-menu-above">
            @Html.ButtonAdd()
        </div>

        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    @ViewBag.Title / @ViewBag.Descr
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>
        <table id="table_receive_parcel" class="table table-striped table-bordered table-hover"
               aria-describedby="table_receive_parcel_info">
            <thead>
                <tr>
                    <th class="center">
                        รหัส
                    </th>
                    <th>@Html.DisplayNameFor(model => model.ReceiveTypeId)</th>
                    <th>@Html.DisplayNameFor(model => model.ReceiveName)</th>
                    <th class="hidden-480">@Html.DisplayNameFor(model => model.DnId)</th>
                    <th class="hidden-480">@Html.DisplayNameFor(model => model.Warehouse.Name)</th>
                    <th class="hidden-480">@Html.DisplayNameFor(model => model.ReceiveDate)</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>

            <tbody>
                @foreach (var m in Model)
                {
                    <tr>
                        <td class="center">
                            @Html.ActionLink(string.IsNullOrWhiteSpace(m.ReceiveCode) ? "-" : m.ReceiveCode, "Details", new { id = m.ReceiveId })
                        </td>

                        <td>@Html.DisplayFor(modelItem => m.ReceiveType.LookUpName)</td>
                        <td>@Html.ActionLink(string.IsNullOrWhiteSpace(m.ReceiveName) ? "-" : m.ReceiveName, "Details", new { id = m.ReceiveId })</td>
                        <td>@Html.DisplayFor(modelItem => m.DeliveryNote.Code)</td>
                        <td>@Html.DisplayFor(modelItem => m.Warehouse.Name)</td>
                        <td>@m.ReceiveDate.ToString("dd/MM/yy")</td>

                        <td class="td-actions">
                            <div class="hidden-phone visible-desktop btn-group">
                                @Html.ButtonDetails(new { id = m.ReceiveId }, true)
                                @Html.ButtonEdit(new { id = m.ReceiveId }, true)

                                @{
                                    var service = new KPIS.GERP.WEB.GIMS.ReceiveParcelService.ReceiveParcelServiceClient();
                                    var item = service.SelectReceiveDtl(new ReceiveDtl { ReceiveId = m.ReceiveId, RecordStatus = "!D" }, null).ToList().Count();
                                    if (item > 0)
                                    {
                                        <a href="#" role="button" class="btn btn-danger btn-mini" aria-disabled="true" onclick="DelEvent()">
                                            <i class="icon-trash bigger-120"></i>
                                        </a>
                                    }
                                    else
                                    {
                                        @Html.ButtonDelete(new { id = m.ReceiveId }, true)
                                    }
                                }
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}


@section scripts {


    <script type="text/javascript">
        function DelEvent() {
            bootbox.alert("ถ้าต้องการลบรายการนี้ กรุณาเข้าไปลบรายละเอียดให้หมดก่อน จึงจะสามารถลบได้");
        }


        $(function () {
            var oTable1 = $('#table_receive_parcel').dataTable(
                {
                    "aoColumns": [
                        { "bSortable": true },
                        { "bSortable": true },
                        { "bSortable": true },
                        { "bSortable": true },
                        { "bSortable": true },
                        { "bSortable": true },
                        { "bSortable": false }
                    ],
                    "aaSorting": [[0, 'desc']],
                });
            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                .each(function () {
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

            });

            $('[data-rel=tooltip]').tooltip();

            $("#form").submit(function (e) {
                e.preventDefault();
                var currentForm = this;
                bootbox.confirm("คุณแน่ใจหรือไม่", function (c) {
                    if (c) {
                        $.ajax({
                            url: "@Url.Action("DeleteAll")",
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            context: document.body,
                            success: function (s) {
                                if (s.success) {
                                    location.reload();
                                }
                            },
                            error: function (xhr, status, error) {
                                alert("Status: " + status + ", Error: " + error + ", XHR: " + xhr.readyState);
                            },
                            statusCode: {
                                404: function () {
                                    alert("Page not found");
                                }
                            }
                        });
                    }
                })
                return false;
            });
        })




    </script>
}
